<template>
  <ContentWrap>
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="120px"
    >
      <el-form-item label="供应商企业全称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入供应商企业全称"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="企业类型" prop="type">
        <el-select
          v-model="queryParams.type"
          placeholder="请选择企业类型"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.COMPLAIN_TYPE)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="企业联系人" prop="companyContact">
        <el-input
          v-model="queryParams.companyContact"
          placeholder="请输入企业联系人"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="企业联系人邮箱" prop="companyEmail">
        <el-input
          v-model="queryParams.companyEmail"
          placeholder="请输入企业联系人邮箱"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="企业联系电话" prop="companyTelephone">
        <el-input
          v-model="queryParams.companyTelephone"
          placeholder="请输入企业联系电话"
          clearable
          @keyup.enter="handleQuery"
          class="!w-240px"
        />
      </el-form-item>
      <el-form-item label="审核状态" prop="status">
        <el-select
          v-model="queryParams.status"
          placeholder="请选择审核状态"
          clearable
          class="!w-240px"
        >
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.COMPLAN_STATUS)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间" prop="createTime">
        <el-date-picker
          v-model="queryParams.createTime"
          value-format="YYYY-MM-DD HH:mm:ss"
          type="daterange"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          :default-time="[new Date('1 00:00:00'), new Date('1 23:59:59')]"
          class="!w-220px"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
        <el-button
          type="primary"
          plain
          @click="openForm('create')"
          v-hasPermi="['erp:supplier:create']"
        >
          <Icon icon="ep:plus" class="mr-5px" /> 新增
        </el-button>
        <el-button
          type="success"
          plain
          @click="handleExport"
          :loading="exportLoading"
          v-hasPermi="['erp:supplier:export']"
        >
          <Icon icon="ep:download" class="mr-5px" /> 导出
        </el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap>
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="序号" align="center" type="index" width="60" />
      <el-table-column label="供应商企业全称" align="left" prop="name" min-width="200" />
      <!-- <el-table-column label="统一社会信用代码" align="center" prop="complainCode" /> -->
      <el-table-column label="注册人姓名" align="left" prop="registryName" min-width="120" />
      <el-table-column label="注册人邮箱" align="left" prop="registryEmail" min-width="150" />
      <el-table-column label="手机号码" align="center" prop="mobile" min-width="120" />
      <!--      <el-table-column label="密码" align="center" prop="password" />-->
      <el-table-column label="企业类型" align="center" prop="type">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COMPLAIN_TYPE" :value="scope.row.type" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="纳税规模" align="center" prop="taxScale">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.TAX_SCALE" :value="scope.row.taxScale" />
        </template>
      </el-table-column> -->
      <!-- <el-table-column label="营业执照含三证合一" align="center" prop="businessPic" /> -->
      <!-- <el-table-column label="法人姓名" align="center" prop="legalName" /> -->
      <!-- <el-table-column label="法人身份证" align="center" prop="legalNo" /> -->
      <!-- <el-table-column label="食品经营许可证" align="center" prop="foodPic" /> -->
      <!-- <el-table-column label="税盘照片" align="center" prop="taxPlatePic" /> -->
      <!-- <el-table-column label="注册资本(万)" align="center" prop="registeredCapital" /> -->
      <el-table-column label="企业联系人" align="center" prop="companyContact" min-width="120" />
      <el-table-column label="企业联系人邮箱" align="center" prop="companyEmail" min-width="150" />
      <el-table-column
        label="企业联系电话"
        align="center"
        prop="companyTelephone"
        min-width="120"
      />
      <!-- <el-table-column label="企业经营范围" align="center" prop="companyScope" /> -->
      <!-- <el-table-column label="企业经营地址" align="center" prop="companyAddress" /> -->
      <el-table-column label="审核状态" align="center" prop="status" min-width="100">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.COMPLAN_STATUS" :value="scope.row.status" />
        </template>
      </el-table-column>
      <!-- <el-table-column label="开户银行" align="center" prop="bankName" /> -->
      <!-- <el-table-column label="收款账号" align="center" prop="bankAccount" /> -->
      <!-- <el-table-column label="开户地址" align="center" prop="bankAddress" /> -->
      <!--      <el-table-column label="创建者" align="center" prop="creator" />-->
      <el-table-column
        label="创建时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="180px"
      />
      <!--      <el-table-column label="更新者" align="center" prop="updater" />-->
      <el-table-column label="操作" align="right" min-width="100px" fixed="right">
        <template #default="scope">
          <el-button
            link
            size="small"
            type="primary"
            @click="openForm('update', scope.row.id)"
            v-hasPermi="['erp:supplier:update']"
          >
            编辑
          </el-button>
          <!-- <el-button
          v-if="scope.row.status === 0"
          link
            type="warning"
            @click="handleReview(scope.row.id)"
            v-hasPermi="['erp:supplier:update']"
          >
            审核
          </el-button> -->
          <!-- <el-button
            v-if="scope.row.status === 1"
            link
            type="warning"
            size="small"
            @click="handleBlack(scope.row.id)"
            v-hasPermi="['erp:supplier:update']"
          >
            黑名单
          </el-button> -->
          <el-button
            v-if="scope.row.status != 0"
            link
            type="danger"
            size="small"
            @click="handleDelete(scope.row.id)"
            v-hasPermi="['erp:supplier:delete']"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>

  <!-- 表单弹窗：添加/修改 -->
  <SupplierForm ref="formRef" @success="getList" />
</template>

<script setup lang="ts">
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
import { dateFormatter } from '@/utils/formatTime'
import download from '@/utils/download'
import { SupplierApi, SupplierVO } from '@/api/erp/purchase/supplier'
import SupplierForm from './SupplierForm.vue'

/** ERP 供应商 列表 */
defineOptions({ name: 'ErpSupplier' })

const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref<SupplierVO[]>([]) // 列表的数据
const total = ref(0) // 列表的总页数
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined,
  type: undefined,
  businessPic: undefined,
  companyContact: undefined,
  companyEmail: undefined,
  companyTelephone: undefined,
  status: undefined,
  createTime: []
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中

/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await SupplierApi.getSupplierPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}

/** 添加/修改操作 */
const formRef = ref()
const openForm = (type: string, id?: number) => {
  formRef.value.open(type, id)
}

/** 删除按钮操作 */
const handleDelete = async (id: number) => {
  try {
    // 删除的二次确认
    await message.delConfirm()
    // 发起删除
    await SupplierApi.deleteSupplier(id)
    message.success(t('common.delSuccess'))
    // 刷新列表
    await getList()
  } catch {}
}

/** 审核按钮操作 */
const handleReview = async (id: number) => {
  try {
    await message.confirm(`您确定提交审核吗？`)
    // 发起审核
    await SupplierApi.reviewSupplier(id)
    message.success('成功提交审核')
    // 刷新列表
    await getList()
  } catch (error) {
    message.error('取消')
  }
}

const handleBlack = async (id: number) => {
  try {
    await message.confirm(`您确定加入黑名单吗？`)
    // 发起审核
    await SupplierApi.addBlack(id)
    message.success('添加黑名单成功')
    // 刷新列表
    await getList()
  } catch (error) {
    message.error('取消')
  }
}

/** 导出按钮操作 */
const handleExport = async () => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await SupplierApi.exportSupplier(queryParams)
    download.excel(data, 'ERP 供应商.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}

/** 初始化 **/
onMounted(() => {
  getList()
})
</script>
